package com.example.icreader;

import android.util.Log;

public class NativeReader {

	private static String TAG = "NativeFunc";
	public static  int    icdev = 0;
	static {
		try {
			System.loadLibrary("tmz");
			Log.d(TAG, "Load TMZ.so succeed!");
		} catch (Throwable e) {
			String sMsg = e.toString();
			Log.e(TAG, sMsg);
		}
	}

	// 打开设备
	// USB

	public static native int ICReaderOpenUsbByFD(int fd);

	//ICReaderOpenUsbByPath 该函数弃用,请使用ICReaderOpenUsbByPathEx
	//public static native int ICReaderOpenUsbByPath(int fd, byte[] mntDevpath);

	public static native int ICReaderOpenUsbByPathEx(int fd, byte[] mntDevpath,int uVid,int uPid);
	
	// COM
	public static native int ICReaderOpenComBypath(byte[] mntDevpath, int iBaud);

	// 关闭设备
	public static native int ICReaderClose(int icdev);

	// 通讯波特率
	public static native int ICReaderSetBaud(int icdev, int module, int iBaud);

	// 获取版本
	public static native int ICReaderGetVer(int icdev, byte[] pVer);

	// 蜂鸣器
	public static native int ICReaderBeep(int icdev, int uMsec, int uDelay,int uNum);

	// 读写序列号
	public static native int ICReaderReadDevSnr(int icdev, int iLen, byte[] pSN);

	// 设备状态
	public static native int ICReaderDevStatus(int icdev, byte[] uStatus);

	// EEPROM
	public static native int ICReaderReadEeprom(int icdev, int iOffset,
												int iLen, byte[] pBuffer);

	public static native int ICReaderWriteEeprom(int icdev, int iOffset,
												 int iLen, byte[] pBuffer);

	// CPU
	public static native int GetCardState(int icdev, int uSlot, int[] uState);

	//
	public static native int GetCardStateEx(int icdev,byte[] uState);

	public static native int ICCSetResetBaud(int icdev, int uSlot, int iBaud);

	public static native int RFCPUSetRate(int icdev, int uSlot, int iRate);

	public static native int CPUPowerOn(int icdev, int uSlot, int iTime,
										byte[] uType, byte[] uSnrLen, byte[] pSnr, byte[] rLen, byte[] pData);

	public static native int CPUPowerOnRate(int icdev, int uSlot, int iTime,
											byte[] uType, byte[] uSnrLen, byte[] pSnr, byte[] rLen, byte[] pData);

	public static native int CPUCardAPDU(int icdev, int uSlot, int sLen,
										 byte[] pSendData, int[] rLen, byte[] pData);
	public static native int CPUCardTrn(int icdev, int uSlot, int sLen,
										byte[] pSendData, int[] rLen, byte[] pData);

	public static native int CPUPowerOff(int icdev, int uSlot);

	// 非接存储卡
	public static native int RFICOpenCard(int icdev, int iTime, byte[] uType,
										  byte[] SnrLen, byte[] pSnr,byte[] pTagSak);

	public static native int RFICAuth(int icdev, int uMode, int uAdr,int uKeylen,
									  byte[] pkey);

	public static native int RFICBlockRead(int icdev, int uAdr, int[] uLen,byte[] pValue);

	public static native int RFICBlockWrite(int icdev, int uAdr,int uLen, byte[] pValue);

	public static native int RFICReadval(int icdev, int uAdr, long[] uiValue);

	public static native int RFICInitval(int icdev, int uAdr, long uiValue);

	public static native int RFICIncrement(int icdev, int uAdr, long uiValue);

	public static native int RFICDecrement(int icdev, int uAdr, long uiValue);

	// 二代证
	// 获取安全模块ID
	public static native int GetGAModeID(int icdev, byte[] uLen, byte[] pIDData);

	// 获取身份证ID
	public static native int GetCardUID(int icdev, byte[] uLen, byte[] pIDData);

	// 获取身份证原始数据
	public static native int ReadIDCardBaseMsg(int icdev, int iFlag,
											   byte[] pucCHMsg, int[] puiCHMsgLen, byte[] pucPHMsg,
											   int[] puiPHMsgLen, byte[] pucFPMsg, int[] puiFPMsgLen);

	/***************************** 接触式存储卡 ***********************/
	// 识别接触式存储卡种类
	/**
	 * @brief 识别接触式存储卡种类
	 *
	 * @param[in] icdev:设备句柄
	 * @param[in] uType:
	01H：SLE4428
	02H：SLE4442
	 * @return 成功等于0，失败返回其他
	 **/
	public static native int ICCheckCardType(int icdev,int uType);

	// 接触式存储卡密钥更改
	public static native int ICPwdChange(int icdev, int uLen,byte[] pPin);

	// 接触式存储卡密码校验
	public static native int ICPwdCheck(int icdev, int uLen,byte[] pPin);

	// 接触式存储卡读数据
	public static native int ICRead(int icdev, int uAdr, int uLen,byte[] pData);

	// 接触式存储卡写数据
	public static native int ICWrite(int icdev,int uAdr, int uLen,byte[] pData);

	// 磁条卡模式设置
	public static native int SetMagCardMode(int icdev, int uMode);

	// 读磁条卡
	public static native int ReadMagCard(int icdev, int timeout,int uMode,
										 int[] uTrack1Len, byte[] pTrack1Data, int[] uTrack2Len,
										 byte[] pTrack2Data, int[] uTrack3Len, byte[] pTrack3Data);

	// 工具函数
	public static native int Hex2Asc(byte[] hex, byte[] asc, int length);

	public static native int Asc2Hex(byte[] asc, byte[] hex, int length);

	public static native int  ReadBankCard(int icdev, int uSlot, byte[] pCardNo,byte[] pCardName);
	//社保卡
	//发卡机构数据文件（EF05）
	//卡识别码|卡的类别|规范版本|初始化机构编号|发卡日期|卡有效期|卡号|
	public static native int ReadSIIssuerInfo(int icdev, int uSlot,byte[] pData);

	//个人基本信息文件(EF06)
	//社会保障号码|姓名|性别|民族|出生地|出生日期|
	public static native int ReadSIPersonalInfo(int icdev,int uSlot,byte[] pData);

	//LED 控制
	public static native int ICReaderLEDCtrl(int icdev,int uLEDCtrl,int uDelay);
	//切换Bootload通道
	public static native int ICReaderNIAPSwitch(int icdev);
	//下载数据
	public static native int ICReaderDownloadFile(int icdev,byte[] pFilePath);

	public static native int ICReaderDownloadDat(int icdev,int datatype, byte[] data,int datalen);

	//T57
	public static native int T5557ReadBlock(int icdev,int uPageNum,int uBlockNum,byte[] pBlockData,int[] BlockDataLen);
	public static native int T5557ReadBlockPwd(int icdev,int uPageNum,int uBlockNum,byte[] pKey,byte[] pBlockData,int[] BlockDataLen);

	public static native int T5557WriteBlock(int icdev,int uPageNum,int uBlockNum,byte[] pBlockData,int BlockDataLen);
	public static native int T5557WriteBlockPwd(int icdev,int uPageNum,int uBlockNum,byte[] pKey,byte[] pBlockData,int BlockDataLen);

	public static native int T5557ReadPage(int icdev,int uPageNum,byte[] pBlockData,int[] BlockDataLen);

	public static native int T5557ReadMoreUID(int icdev,byte[] UIDData,int[] BlockDataLen,int[] CardType);

	//15693卡
	public static native int RFIDCard(int icdev,byte[] pUID,int[] UIDLen);
	//
	public static native int RFFind15693(int icdev,byte[] pUID,int[] UIDLen);
	public static native int RFSelect15693(int icdev,byte[] pUID,int UIDLen);
	public static native int RFRead15693(int icdev,int addr,byte[] pData);
	public static native int RFWrite15693(int icdev,int addr,byte[] pData);

	//扫码
	public static native int GetScanCodeVer(int icdev,byte[] pVer);

	public static native int SetScanCodeMode(int icdev,int uMode);

	public static native int GetScanCodeMode(int icdev,int[] uMode);
	public static native int GetScanCodeData(int icdev,int iTime,int[] ilen,byte[] pCode);

	public static native int KBGetVer(int icdev,byte[] pVer);

	public static native int KBGetKBVoice(int icdev,int iVoice);

	public static native int KBGetPin(int icdev,int uPinlen,int uTimeout,byte[] pPin);
	
	//加密机读三代卡认证信息
	public static native int ReadSIInfoHSM1(int icdev,int uslot,int iAuthType,byte[] pAuthData);
	//卡识别码|卡的类别|规范版本|初始化机构编号|发卡日期|卡有效期|卡号|社会保障号码|姓名|性别|民族|出生地|出生日期|
	public static native int ReadSIInfoHSM2(int icdev,int uslot,byte[] pKey,byte[] pInfo);
	
	//
	public static native int KBDownMainkey(int icdev,int iDestype,int iMainkeyNo,byte[] pMainkey);
	public static native int KBDownUserkey(int icdev,int iDestype,int iMainkeyNo,int iUserkeyNo,byte[] pUserkey);
	public static native int KBActiveKey(int icdev,int iMainkeyNo,int iUserkeyNo);
	public static native int KBGetEnpin(int icdev,int uPinlen,int uTimeout,byte[] pCardNo,byte[] pPin,byte[] uKeyNum);

	//自动读卡
	public static native int SetAutoReadCard(int icdev,int uMode);
	public static native int ReaderSetPerCom(int icdev,int PerCom1,int PerCom2);

	//设置插卡蜂鸣
	public static native int SetInsertCardBeep(int icdev,int uMode);
	public static native int KBSetMode(int icdev, int uMode);
	public static native int KBSetDestroy(int icdev, int uMode);
	public static native int IDCardControl(int icdev,byte[] pSendData,int SendLen,byte[] pRecvData,int[] RecvLen);
	public static native int SamAControl(int icdev,byte[] pSendData,int SendLen,byte[] pRecvData,int[] RecvLen);
	public static native byte[] GetReaderErrInfo(int errCode);
}
